//
//  AppDelegate.m
//  chebabaGYS
//
//  Created by zhengminjie on 15/12/24.
//  Copyright © 2015年 zhengminjie. All rights reserved.
//
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>

#import "AppDelegate.h"
#import "APService.h"

@interface AppDelegate ()<BMKGeneralDelegate> {
    
    BMKMapManager *_mapManager;
    
}


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [_mapManager start:@"kVbP8ry8tZbVbMxBP8PGVKt5"  generalDelegate:self];
    if (!ret) {
        NSLog(@"地图启动失败");
    }
    
    
    
    
    // Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                       UIUserNotificationTypeSound |
                                                       UIUserNotificationTypeAlert)
                                           categories:nil];
    }
//    else {
//        //categories 必须为nil
//        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
//                                                       UIRemoteNotificationTypeSound |
//                                                       UIRemoteNotificationTypeAlert)
//                                           categories:nil];
//    }
//    
    // Required
    [APService setupWithOption:launchOptions];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkDidLogin:) name:kJPFNetworkDidLoginNotification object:nil];
    
    
    
    
    
    
    
    
    
    
    
    
    return YES;
}


- (void)onGetNetworkState:(int)iError{
    
    if (iError) {
        NSLog(@"联网失败");
    }else{
        NSLog(@"联网成功");
    }
}
- (void)onGetPermissionState:(int)iError{
    
    if (iError) {
        NSLog(@"授权失败 %d",iError);
    }else{
        NSLog(@"授权成功");
    }
}


- (void)netWorkDidLogin:(NSNotification *)notification{
    
    NSString *registerId = [APService registrationID];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:registerId forKey:@"registerId"];
    
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Required
    //NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
    [APService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required
    NSLog(@"收到消息");
    NSLog(@"userInfo:%@",userInfo);
    [APService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
    // IOS 7 Support Required
    
    
    
    NSLog(@"收到消息");
    
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    [BMKMapView willBackGround];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [BMKMapView willBackGround];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
